<?

require_once('Zend/Auth.php');
require_once('Gpc/User/Adapter/Abstract.php');

class Gpc_User_Adapter_Default extends Gpc_User_Adapter_Abstract {

  // Tables used for basic user/pass authentication
  protected $tableName        = 'users';
  protected $identityColumn   = 'username';
  protected $credentialColumn = 'password';

  // Where all the defaults like cookie name is.
  protected $settingsFile = 'Settings.php';

  // This is how we convert SMF data to GPC data.
  // Add what ever you want to map from the user/member table here.
  //  However, all data is still made available through the "rawData" key.
  private $identityMap = array ( 'id' => 'id',
                                 'username' => 'username', 
                                 'firstname' => 'name',
                                 'meail' => 'email',
                                 'password' => 'password' ); 

  /**
   * Constructor
   */
  public function __construct() {
    parent::__construct(); // Required! Loads some config info

    $file = $this->forumPath . $this->settingsFile;

    // Load the settings...
    include(WWW_ROOT . DS . $file);
    $this->cookieName = $cookiename;
  }

  /**
   * Sets the password, applying any required salting/hashing used by the forum.
   *
   * @param string User's password
   */
  protected function _setPassword( $password ) {
    $this->password = sha1($this->username . $password);
  }

  protected function _setUsername( $username ) {
    $this->username = strtolower($username);
  }

  public function checkForCookie() {
    // Auth Idendity. If not, check for Cookie. If cookie is there, get data from DB
    $auth = Zend_Auth::getInstance();
    if ( ! $auth->hasIdentity() &&
           isset($_COOKIE[$this->cookieName]) ) {
      list( $userId, $passwd ) = unserialize( $_COOKIE[$this->cookieName] );

      // Reload the user data
      $this->_loadIdentity($userId, $passwd);
    }
  }

  public function logout() {
    setCookie($this->cookieName, '', time()-3600, '/');
    parent::logout();
  }

  public function mapIdentity( $dbRow ) {
    foreach ( $dbRow as $key => $val ) {
      if ( isset($this->identityMap[$key]) ) {
        $this->identity->{$this->identityMap[$key]} = $val;
      }
    }

    // Go ahead and store the raw data incase this class needs access to any of it
    $this->identity->rawData = $dbRow;
  }

  protected function _setCookie() {
    $identity = Zend_Auth::getInstance()->getIdentity();
    $dataArray = array( $identity->id,
                        sha1($this->getPassword() . $identity->rawData->passwordSalt),
                        time()+85400,
                        2 );

    setCookie($this->cookieName, serialize($dataArray), time() + 60*60*4, '/');
  }

  protected function _loadIdentity( $userId, $passwd ) {
    $db = Zend_Registry::get('db');
    $db->setFetchMode(Zend_Db::FETCH_OBJ);

    $result = $db->fetchRow('SELECT * FROM ' . $this->tableName . ' WHERE ID_MEMBER=' . intval($userId));
    $this->_translateIdentity($result);
  }
}
